//
//  Status.swift
//  sinaDemo
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit

class Status: NSObject {
    // 微博创建时间
    var created_at: String?
    
    // 微博ID
    var id: Int64 = 0
    
    // 微博内容
    var text: String?
    
    // 微博富文本内容
    var attributedText: NSAttributedString? {
        // 用正则表达式来获取里面的表情文字,然后再去取出对应的表情图片,然后再转换成富文本展示在cell上
        // 规则
        let pattern = "\\[.*?\\]"
        let regx = try! NSRegularExpression(pattern: pattern, options: [])
        // 结果集
        let results = regx.matchesInString(text ?? "", options: [], range: NSMakeRange(0, (text ?? "").characters.count))
        // 倒叙,取出表情文字
        var index = results.count - 1
        let strM = NSMutableAttributedString(string: text ?? "")
        while index >= 0 {
            let result = results[index]
            let range = result.rangeAtIndex(0)
            let subStr = (text ?? "" as NSString).substringWithRange(range)
            // 去表情库里查找图片
            if let emoticon = XBEmoticonManager.sharedManager.getEmoticonWithString(subStr) {
                // 获取表情富文本
                let imageAttach = XBEmoticonAttachment(emoticon: emoticon).imageText(UIFont.systemFontOfSize(statusCellNameFont))
                // 将上面范围内的字符串转换成富文本
                strM.replaceCharactersInRange(range, withAttributedString: imageAttach)
            }
            index--
        }
        return strM
    }
    
    // 微博来源
    var source: String?
    
    // 用户
    var user: User?
    
    // 配图urls数组
    var imageUrls: [NSURL]?
    
    // 配图的数组
    var pic_urls: [[String : String]]? {
        didSet {
            guard let array = pic_urls else {
                return
            }
            
            // 遍历数组,取出字典,转换成url
            // 先实例化数组
            imageUrls = [NSURL]()
            for item in array {
                let urlStr = item["thumbnail_pic"]!
                let url = NSURL(string: urlStr)!
                imageUrls!.append(url)
            }
        }
    }
    
    // 转发微博
    var retweeted_status: Status?
    
    // 构造方法
    //构造方法 KVC设置值
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    // 因为在属性里还有其他的模型
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String : AnyObject] {
                user = User(dict: dict)
                print(user)
            }
            // 需要return,如果是user得属性,那么直接return
            return
        }
        
        if key == "retweeted_status" {
            if let dict = value as? [String : AnyObject] {
                retweeted_status = Status(dict: dict)
                print(retweeted_status)
            }
            // 需要return,如果是user得属性,那么直接return
            return
        }
        // 调用super的这个方法,完成字典转模型
        super.setValue(value, forKey: key)
    }
    
    // 过滤没有的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        let keys = ["created_at", "id", "text", "source", "user"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
